package com.food.restaurantpro.utils;

import com.food.restaurantpro.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AlertDialogManager {
	/**
	 * 
	 * @param activity
	 * @param callback : interface to process events when you clicked yes and no
	 * @param optionNo = true: "No" button visible, else gone
	 * @param optionYes = true: "Yes" button visible, else gone
	 * @param title
	 * @param message
	 * @param yes : text for "Yes" button
	 * @param no : text for "No" button
	 * @param status = true: show icon success, else show icon fail
	 */
	public static void showAlertDialog(final Activity activity,
			final AlertDialogInterface callback, boolean optionNo,
			boolean optionYes, String title, String message, String yes,
			String no, boolean status) {
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		if(status) {
			builder.setIcon(R.drawable.success);
		} else {
			builder.setIcon(R.drawable.fail);
		}
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setCancelable(false);
		if (optionNo) {
			builder.setNegativeButton(no,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
							callback.onChooseNoCallback();
						}
					});
		}
		if (optionYes) {
			builder.setPositiveButton(yes,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							callback.onChooseYesCallback();
						}
					});
		}

		AlertDialog alert = builder.create();
		alert.show();
	}
	
	
	/**
	 * 
	 * @param activity
	 * @param callback : interface to process events when you clicked yes and no
	 * @param title
	 * @param message
	 * @param yes : text for "Yes" button
	 * @param no : text for "No" button
	 */
	static Dialog dialog;
	public static void showCustomDialog(final Activity activity,
			final AlertDialogInterface callback, String title, String message, String yes,
			String no, boolean optionCloseButton) {
		//
		LayoutInflater inflater = activity.getLayoutInflater();
		View dialoglayout = inflater.inflate(R.layout.custom_dialog, null);
		//
		dialog = null;
		dialog = new Dialog(activity);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		dialog.setContentView(dialoglayout);
		dialog.setCanceledOnTouchOutside(false);
		dialog.setCancelable(false);
		
		//
		Button btnCancel = (Button) dialoglayout.findViewById(R.id.btnDialogCancel);
		Button btnYes = (Button) dialoglayout.findViewById(R.id.btnDialogYes);
		Button btnClose = (Button) dialoglayout.findViewById(R.id.btnDialogExit);
		TextView tvTitle = (TextView) dialoglayout.findViewById(R.id.tvDialogTitle);
		TextView tvContent = (TextView) dialoglayout.findViewById(R.id.tvDialogContent);
		tvTitle.setText(title);
		tvContent.setText(message);
		btnCancel.setText(no);
		btnYes.setText(yes);
		if(optionCloseButton == false) {
			btnClose.setVisibility(View.GONE);
		} else {
			btnClose.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.cancel();
					callback.onChooseNoCallback();
				}
			});
		}
		
		btnCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
				callback.onChooseNoCallback();
			}
		});
		btnYes.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				 dialog.cancel();
				 callback.onChooseYesCallback();
			}
		});
		
		//
		dialog.show();
	}
}
